package MainPackage;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.*;
import javax.swing.JOptionPane;


/**
 *
 * @author d.chauvistre
 */
public class Registration extends javax.swing.JDialog {

    /**
     * Creates new form Registration
     */
    
    //Liste mit Usern
    private Userlist userlist;
    
    
    /**
     * Konstruktor für Fenster zur Registration
     * @param userlist aktuelle Liste bereits registrierter User
     */
    public Registration(Userlist userlist) {
        this.userlist = userlist;
        initComponents();
        this.setModal(true);
        this.setVisible(true);
    }
    /**
     * Erstellt neues Registration Fenster
     * @param userlist aktuelle Liste bereits registrierter User
     */
    public static void ShowRegistration(Userlist userlist){
        Registration reg = new Registration(userlist);
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        name = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        groesseText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        registrieren = new javax.swing.JToggleButton();
        geschlechtText = new javax.swing.JComboBox();
        arbeitText = new javax.swing.JComboBox();
        passwort = new javax.swing.JPasswordField();
        passwort_erneut = new javax.swing.JPasswordField();
        tag = new javax.swing.JComboBox();
        monat = new javax.swing.JComboBox();
        jahr = new javax.swing.JTextField();

        setTitle("Registrierung");
        setAlwaysOnTop(true);
        setModal(true);
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
        setResizable(false);

        jLabel1.setText("Name");

        jLabel2.setText("Größe in cm");

        jLabel4.setText("Geburtsdatum");

        jLabel5.setText("Geschlecht");

        jLabel6.setText("Arbeit");

        jLabel7.setText("Passwort");

        jLabel8.setText("Passwort erneut");

        registrieren.setText("Ok");
        registrieren.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registrierenActionPerformed(evt);
            }
        });

        geschlechtText.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "männlich", "weiblich" }));

        arbeitText.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "vornehmlich stehend", "vornehmlich sitzend", "körperlich" }));

        passwort.setText("jPasswordField1");
        passwort.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                passwortMouseClicked(evt);
            }
        });

        passwort_erneut.setText("jPasswordField1");
        passwort_erneut.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                passwort_erneutMouseClicked(evt);
            }
        });

        tag.setMaximumRowCount(31);
        tag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        monat.setMaximumRowCount(12);
        monat.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1)
                                .addComponent(jLabel5)
                                .addComponent(jLabel6)
                                .addComponent(jLabel7)
                                .addComponent(jLabel8))
                            .addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(0, 6, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(registrieren, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(192, 192, 192))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(tag, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(monat, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jahr, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(name, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                        .addComponent(groesseText, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(arbeitText, javax.swing.GroupLayout.Alignment.TRAILING, 0, 221, Short.MAX_VALUE)
                        .addComponent(geschlechtText, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(passwort, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwort_erneut, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(66, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(groesseText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(monat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jahr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(geschlechtText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(arbeitText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwort_erneut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addComponent(registrieren)
                .addContainerGap(62, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**Überprüft eingegebene Daten und erstellt bei Korrektheit einen neuen User
     * 
     * @param evt 
     */
    private void registrierenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registrierenActionPerformed
        double gewicht = 0;
        int groesse = 0, jahr = 0;
        User.Arbeit arbeit = User.Arbeit.koerperlich;
        User.Geschlecht geschlecht= User.Geschlecht.maennlich;
        String passwd1, passwd2;
        
        /**
         * Überprüfen verschiedener Werte und ggf. Fehlermeldung
         */
        try {
            groesse = Integer.parseInt(this.groesseText.getText());
            jahr = Integer.parseInt(this.jahr.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Falsche Eingabe!", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (jahr < 1900 || jahr > 2012) {
            JOptionPane.showMessageDialog(this, "Falsche Jahreseingabe!", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        /**
         * Geburtstag als Datum speichern
         */
        Calendar cal = Calendar.getInstance();
        cal.set(jahr, (this.monat.getSelectedIndex()), (this.tag.getSelectedIndex()+1));
        Date gebdate= cal.getTime();

        /**
         * Geschlecht speichern
         */
        if (this.geschlechtText.getSelectedIndex() == 0) {
            geschlecht = User.Geschlecht.maennlich;//männlich
        } else if (this.geschlechtText.getSelectedIndex() == 1) {
            geschlecht = User.Geschlecht.weiblich;//weiblich
        } else {
            JOptionPane.showMessageDialog(this, "Geschlecht auswählen!", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        /**
         * Arbeit speichern
         */
        if (arbeitText.getSelectedIndex() == 0) {
            arbeit = User.Arbeit.vornehmlichStehend;
        } else if (arbeitText.getSelectedIndex() == 1) {
            arbeit = User.Arbeit.vornehmlichSitzen;
        } else if (arbeitText.getSelectedIndex() == 2) {
            arbeit = User.Arbeit.koerperlich;
        } else {
            JOptionPane.showMessageDialog(this, "Arbeit auswählen!", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        /**Passwort speichern.
         * Überprüfen ob User bereits vorhanden oder Passwort nicht übereinstimmt -> Fehlermeldung
         */
        passwd1 = new String(this.passwort.getPassword());
        passwd2 = new String(this.passwort_erneut.getPassword());
        
        if(userlist.enthaeltUser(this.name.getText())){
            JOptionPane.showMessageDialog(this, "Name bereits vergeben. Neue Eingabe!", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }else if(!passwd1.equals(passwd2)){
            JOptionPane.showMessageDialog(this, "Passwort stimmt nicht überein. Erneute Eingabe!", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        /**
         * User erstellen und userlist hinzufügen
         */
        User u = new User(this.name.getText(), passwd1, gebdate, groesse, geschlecht, arbeit);                          
        userlist.addUser(u);
        u.ReferenzwerteNeuBerechnen();
        
        //speichern
        userlist.export();
        
        JOptionPane.showMessageDialog(this, "Ihr Benutzer wurde erfolgreich erstellt.\nBitte loggen Sie sich nun mit ihren eingegebenen Daten ein.", "Info", JOptionPane.INFORMATION_MESSAGE);
        this.setVisible(false);        
    }//GEN-LAST:event_registrierenActionPerformed

    private void passwortMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_passwortMouseClicked
       this.passwort.setText(null);
    }//GEN-LAST:event_passwortMouseClicked

    private void passwort_erneutMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_passwort_erneutMouseClicked
        this.passwort_erneut.setText(null);
    }//GEN-LAST:event_passwort_erneutMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox arbeitText;
    private javax.swing.JComboBox geschlechtText;
    private javax.swing.JTextField groesseText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField jahr;
    private javax.swing.JComboBox monat;
    private javax.swing.JTextField name;
    private javax.swing.JPasswordField passwort;
    private javax.swing.JPasswordField passwort_erneut;
    private javax.swing.JToggleButton registrieren;
    private javax.swing.JComboBox tag;
    // End of variables declaration//GEN-END:variables
}
